//
//  XYHTTPTool.swift
//  WeiboSourceCode
//
//  Created by 周星宇 on 15/12/29.
//  Copyright © 2015年 cn.weibo. All rights reserved.
//

import UIKit
import AFNetworking

enum XYRequestMethod: String {
    
    case GET = "GET"
    case POST = "POST"
}
class XYHTTPTool: AFHTTPSessionManager {
    
    // MARK: 单例
    static let sharedHttpTool: XYHTTPTool = {
        
        let instence = XYHTTPTool()
        instence.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return instence
    }()
    
    // MARK: 请求回调的别名
    typealias XYRequestCallBack = (response: AnyObject?, error: NSError?)->()
    
    func request(method:XYRequestMethod = .GET,
         urlString: String,
         params: AnyObject?,
         finished: XYRequestCallBack){
        
        if method == .GET{
            
            GET(urlString,
                parameters: params,
                progress: nil,
                success: { (_, responseObj) -> Void in
                    finished(response: responseObj, error: nil)
                },
                failure: { (_, error) -> Void in
                    finished(response: nil, error: error)
            })
        }else{
            POST(urlString,
                parameters: params,
                progress: nil,
                success: { (_, responseObj) -> Void in
                    finished(response: responseObj, error: nil)
                },
                failure: { (_, error) -> Void in
                    finished(response: nil, error: error)
            })
        }
    }
}
// MARK: 封装请求token  和  用户信息的逻辑
extension XYHTTPTool {
    
    func requestLoadAccount(code:String,finish:XYRequestCallBack) {
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        let params = [
            
            "client_id": AppKey,
            "client_secret": AppSecret,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": redirect_uri
        ]
        request(.POST, urlString: urlString, params: params, finished: finish)
    }
    func requestLoadUserInfo(userAccount:XYUserAccount,finish:XYRequestCallBack){
        
        let urlString = "https://api.weibo.com/2/users/show.json"
        // 请求参数
        let params = [
            "access_token": userAccount.access_token!,
            "uid": userAccount.uid!
        ]
        request(urlString: urlString, params: params, finished: finish)
    }
}
// MARK: 封装首页数据请求逻辑
extension XYHTTPTool {
    
    func requestLoadHomeData(maxId: Int64,sinceId:Int64,finish:XYRequestCallBack) {
        
        let urlString = "https://api.weibo.com/2/statuses/friends_timeline.json"
        let params = [
            "access_token": XYUserAccountViewModel.sharedUserAccountViewModel.accessToken!,
            "max_id": "\(maxId)",
            "since_id": "\(sinceId)"
        ]
        request(urlString: urlString, params: params, finished: finish)
    }
}
extension XYHTTPTool {
    
    func requestSendText(accessToken: String,status: String, finished:XYRequestCallBack) {
        
        let urlString = "https://api.weibo.com/2/statuses/update.json"
        let params = [
            "access_token": accessToken,
            "status": status
        ]
        request(.POST, urlString: urlString, params: params, finished: finished)
    }
}